package com.gc;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * Created by Administrator on 2019/5/27.
 */
public class MyListener {
    public static void main(String[] args) throws IOException {

        ServerSocket server = new ServerSocket(8080);
        Socket client = server.accept();
        System.out.println("客户端："+client.getLocalAddress().getHostName()+"发送请求");
        InputStream is = client.getInputStream();
        System.out.println("数据长度："+is.available());
        byte[] bytes = new byte[1024];
        int length = is.read(bytes);
        System.out.println("请求数据："+new String(bytes,0,length));

        System.out.println("开始响应");
        OutputStream os = client.getOutputStream();
        String html = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<html><body>helloworld!</body></html>";
        os.write(html.getBytes());
        is.close();
        os.close();
        System.out.println("响应结束");
    }
}
